package com.xinqi.common.third.util;

import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;

public class FileUtils {

    /**
     * 网络文件转文件流
     *
     * @author dzy
     */
    public static InputStream urlToStream(String urlPath) {
        try {
            URL url = new URL(urlPath); // 替换为实际的文件URL
            URLConnection connection = url.openConnection();
            InputStream inputStream = connection.getInputStream();
            return inputStream;
        } catch (IOException e) {
            return null;
        }
    }

    /**
     * 获取路径文件后缀名
     */
    public static String getSuffix(String url) {
        String[] strArray = url.split("\\.");
        int suffixIndex = strArray.length - 1;
        return strArray[suffixIndex];
    }

    /**
     * 获取中文URL编码
     */
    public static String getFileName(String url) {
        String fileName = url.substring(url.lastIndexOf("/") + 1);
        String urlFileName = URLEncoder.encode(fileName);
        return url.replace(fileName, urlFileName);
    }


}
